<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$conf = new Config();
$lotto_id = intval($_GET['id']);
$bonum = intval($_GET['bonum']);
$order_id = intval($_GET['order']);
$startbet = $conf->get("startbet");
$stopbet = $conf->get("waitbet");
$prot_start = $conf->get("protect_start");
if (!isset($_SESSION['bsaystartmod']))
{
    $_SESSION['bsaystartmod'] = 0;
}
$saystartmod = $_SESSION['bsaystartmod'];
if (!isset($_SESSION['bsaywaitmod']))
{
    $_SESSION['bsaywaitmod'] = 0;
}
$saystopmod = $_SESSION['bsaywaitmod'];
if ($stopbet <= date("Hi") && !$saystopmod)
{
    $saystopmod = 1;
    $_SESSION['bsaywaitmod'] = $saystopmod;
    echo "-1";
    exit();
}
$stopmode = $prot_start == 0 ? 1 : $saystopmod;
if ($saystopmod == 0 && $prot_start != $saystartmod)
{
    $saystartmod = $prot_start;
    $_SESSION['bsaystartmod'] = $saystartmod;
    $stopmode = $saystartmod;
    echo "-1";
    exit();
}
$ary = array();
$ary[$order_id] = array();
$ary[$order_id][25] = array();
$ary[$order_id][26] = array();
$i = 1;
while ($i < 50)
{
    $ary[$order_id][25][$i] = array(
        "num" => $i,
        "rate" => 0,
        "amount" => 0,
        "count" => 0,
        "lost" => 0
    );
    $ary[$order_id][26][$i] = array(
        "num" => $i,
        "rate" => 0,
        "amount" => 0,
        "count" => 0,
        "lost" => 0
    );
    ++$i;
}
$total_win = array();
$total_win[$order_id] = array();
$total_win[$order_id][25] = 0;
$total_win[$order_id][26] = 0;
$usedamount = 0;
$theacl = $curruser['acl'];
$userid = $curruser['id'];
$userlist = GetUserList($userid);
if ($userlist == "")
{
    exit("Not Users!");
    exit();
}
$rs = getdb("select sum(amount) from bet2 where user_id=" . $userid . " and lotto_id=" . $lotto_id . " and rame in (0,9)");
$usedamount = $rs->f[0] ? $rs->f[0] : 0;
$rs->close();
$rs = getdb("select SUM(amount_6h) from `k_user_bet` where user_id in ({$userlist}) and lotto_id={$lotto_id}");
$usedamount += $rs->f[0] ? $rs->f[0] : 0;
$rs->close();
$usedamount = $curruser['amount'] - $usedamount - 0;
$sql = "select * from bet where lotto_id=" . $lotto_id . " and bettype in (25,26) and numbers like '" . $order_id . " %' and rame in (0,9)";
$sql .= " and user_id in (" . $userlist . ") order by bettype";
if ($bonum < $curruser['alert40'])
{
    getdb("update k_user set alert40=" . $bonum . " where id=" . $curruser['id']);
}
$rs = getdb($sql);
while (!$rs->eof)
{
    $backs = 100;
    if ($theacl == 5)
    {
        $backs = $rs->f['back4'];
        $percent = $rs->f['percent4'];
    }
    else if ($theacl == 4)
    {
        $backs = $rs->f['back3'];
        $percent = $rs->f['percent3'];
    }
    else if ($theacl == 3)
    {
        $backs = $rs->f['back2'];
        $percent = $rs->f['percent2'];
    }
    else
    {
        $backs = $rs->f['back1'];
        $percent = $rs->f['mpercent'];
    }
    if (0 < $percent)
    {
        $percent /= 100;
    }
    $tmp = $percent * $rs->f['amount'];
    $p = explode(" ", $rs->f['numbers']);
    if (intval($p[0]) == $order_id)
    {
        $num = intval($p[1]);
        $ary[$order_id][$rs->f['bettype']][$num]['amount'] += $tmp;
        ++$ary[$order_id][$rs->f['bettype']][$num]['count'];
        $ary[$order_id][$rs->f['bettype']][$num]['lost'] += $tmp * $rs->f['rate'];
        $total_win[$order_id][$rs->f['bettype']] += $tmp * zero($backs, 100);
    }
    $rs->movenext();
}
$rs->close();
if (2 < $theacl)
{
    $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and uperid" . $theacl . "=" . $userid . " and bettype in (25,26) and numbers like '" . $order_id . " %' and rame in (0,9) order by bettype";
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $backs = 100;
        if ($theacl == 5)
        {
            $backs = $rs->f['back4'];
            $percent = $rs->f['percent5'];
        }
        else if ($theacl == 4)
        {
            $backs = $rs->f['back3'];
            $percent = $rs->f['percent4'];
        }
        else if ($theacl == 3)
        {
            $backs = $rs->f['back2'];
            $percent = $rs->f['percent3'];
        }
        if (0 < $percent)
        {
            $percent /= 100;
        }
        if ($rs->f['prev_id'] == 0)
        {
            $percent = 0;
        }
        $tmp = $percent * $rs->f['amount'];
        $p = explode(" ", $rs->f['numbers']);
        if (intval($p[0]) == $order_id)
        {
            $num = intval($p[1]);
            $ary[$order_id][$rs->f['bettype']][$num]['amount'] += $tmp;
            ++$ary[$order_id][$rs->f['bettype']][$num]['count'];
            $ary[$order_id][$rs->f['bettype']][$num]['lost'] += $tmp * $rs->f['rate'];
            $total_win[$order_id][$rs->f['bettype']] += $tmp * zero($backs, 100);
        }
        $rs->movenext();
    }
}
$sql = "select * from bet2 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and bettype in (25,26) and numbers like '" . $order_id . " %' and rame in (0,9) order by bettype";
$rs = getdb($sql);
while (!$rs->eof)
{
    $backs = 100;
    if ($theacl == 5)
    {
        $backs = $rs->f['back5'];
    }
    else if ($theacl == 4)
    {
        $backs = $rs->f['back4'];
    }
    else if ($theacl == 3)
    {
        $backs = $rs->f['back3'];
    }
    else if ($theacl == 2)
    {
        $backs = $rs->f['back2'];
    }
    $tmp = $rs->f['amount'];
    $p = explode(" ", $rs->f['numbers']);
    if (intval($p[0]) == $order_id)
    {
        $num = intval($p[1]);
        $ary[$order_id][$rs->f['bettype']][$num]['amount'] -= $tmp;
        $ary[$order_id][$rs->f['bettype']][$num]['lost'] -= $tmp * $rs->f['rate'];
        $total_win[$order_id][$rs->f['bettype']] -= $tmp * zero($backs, 100);
    }
    $rs->movenext();
}
$rs->close();
$j = 1;
while ($j < 50)
{
    $ary[$order_id][25][$j]['amount'] += $ary[$order_id][26][$j]['amount'];
    $ary[$order_id][25][$j]['count'] += $ary[$order_id][26][$j]['count'];
    $ary[$order_id][25][$j]['lost'] += $ary[$order_id][26][$j]['lost'];
    ++$j;
}
$total_win[$order_id][25] += $total_win[$order_id][26];
$rs = getdb("select * from rate4" . $order_id . "a where lotto_id=" . $lotto_id);
if ($rs->eof)
{
    exit("Error ID!");
}
$rsu = get_srates_data($userid, $lotto_id, 25, $order_id);
if ($rsu->eof)
{
    exit("Error ID!");
}
$i = 1;
while ($i < 50)
{
    $ary[$order_id][25][$i]['rate'] = $rs->f["r" . d2($i)] - $rsu->f["r" . d2($i)] - 0;
    ++$i;
}
$rs->close();
$i = 1;
while ($i < 49)
{
    $j = $i + 1;
    while ($j < 50)
    {
        if ($ary[$order_id][25][$i]['lost'] < $ary[$order_id][25][$j]['lost'])
        {
            $tmp = $ary[$order_id][25][$i];
            $ary[$order_id][25][$i] = $ary[$order_id][25][$j];
            $ary[$order_id][25][$j] = $tmp;
        }
        ++$j;
    }
    ++$i;
}
echo $usedamount . "@" . $total_win[$order_id][25];
$i = 1;
while ($i < 50)
{
    echo "," . $ary[$order_id][25][$i]['num'] . "@" . $ary[$order_id][25][$i]['rate'] . "@" . $ary[$order_id][25][$i]['count'] . "@" . $ary[$order_id][25][$i]['amount'];
    echo "@" . $ary[$order_id][25][$i]['lost'] . "@" . $stopmode;
    ++$i;
}
echo "\n";
include( "class_6h/class.menu.php" );
$fieldArr = array(
    "lotto_id" => $lotto_id,
    "split_id" => 1
);
$onlineMenu = new menu_6h(12, $fieldArr);
echo $onlineMenu->getMenuAmu();
echo "\n";
exit();